package org.web.webdisplay.server.biz.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.web.base.domain.InsertGroup;
import org.web.base.domain.ResultDO;
import org.web.base.domain.UpdateGroup;
import org.web.base.domain.exception.ResultMessageEnum;
import org.web.base.domain.exception.ServiceException;
import org.web.base.domain.helper.ListHelper;
import org.web.base.domain.helper.ResultHelper;
import org.web.base.domain.helper.ServiceExceptionHelper;
import org.web.base.domain.helper.ValidationHelper;
import org.web.webdisplay.sdk.biz.AccountInfoBiz;
import org.web.webdisplay.sdk.dto.AccountInfoDTO;
import org.web.webdisplay.sdk.dto.query.QueryAccountInfoRequest;
import org.web.webdisplay.server.domain.AccountInfoDO;
import org.web.webdisplay.server.domain.query.QueryAccountInfo;
import org.web.webdisplay.server.service.AccountInfoService;

//import jakarta.annotation.Resource;
import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("accountInfoBiz")
public class AccountInfoBizImpl implements AccountInfoBiz {

    @Resource
    private AccountInfoService accountInfoService;

    @Override
    public ResultDO<List<AccountInfoDTO>> selectAclResourceList(@RequestBody QueryAccountInfoRequest queryAccountInfoRequest) {
        ResultDO<List<AccountInfoDTO>> resultDO = new ResultDO<>();
        QueryAccountInfo queryAccountInfo = new QueryAccountInfo();
        try {
            queryAccountInfoRequest.checkAndBuildQuery();
            BeanUtils.copyProperties(queryAccountInfoRequest, queryAccountInfo);
            ResultDO<List<AccountInfoDO>> selectResult = accountInfoService.selectAccountInfoListResult(queryAccountInfo);
            if (selectResult.isSuccess()) {
                List<AccountInfoDTO> list = new ArrayList<>();
                resultDO.setDomain(list);
                if (selectResult.getDomain() != null && selectResult.getDomain().size() > 0) {
                    for (AccountInfoDO aclResourceDetailDO : selectResult.getDomain()) {
                        AccountInfoDTO accountInfoDTO = new AccountInfoDTO();
                        BeanUtils.copyProperties(aclResourceDetailDO, accountInfoDTO);
                        list.add(accountInfoDTO);
                    }
                }
                resultDO.setTotalCount(selectResult.getTotalCount());
            } else {
                throw ServiceExceptionHelper.buildServiceException(selectResult);
            }
        } catch (Exception e) {
            resultDO = ResultHelper.buildResultDOByException(e);
        }
        return resultDO;
    }

    @Override
    public ResultDO<AccountInfoDTO> saveAccountInfo(AccountInfoDTO accountInfoDTO) {
        ResultDO<AccountInfoDTO> resultDO = new ResultDO<>();
        resultDO.setDomain(accountInfoDTO);
        try {
            if (accountInfoDTO == null) {
                throw ServiceExceptionHelper.buildServiceException(ResultMessageEnum.PARAM_EMPTY, "保存失败，参数不能为空");
            }
            if (accountInfoDTO.getAccountInfoId() != null) {
                ValidationHelper.validateWithThrow(AccountInfoDTO.class, accountInfoDTO, UpdateGroup.class);
                AccountInfoDO accountInfoDO = new AccountInfoDO();
                BeanUtils.copyProperties(accountInfoDTO, accountInfoDO);
                int updateCount = accountInfoService.updateAccountInfoByAccountInfoId(accountInfoDO);
                if (updateCount > 0) {
                    resultDO.setSuccess(true);
                    BeanUtils.copyProperties(accountInfoDO, accountInfoDTO);
                    resultDO.setDomain(accountInfoDTO);
                } else {
                    resultDO.setSuccess(false);
                    resultDO.setMessage("更新失败");
                }
            } else {
                ValidationHelper.validateWithThrow(AccountInfoDTO.class, accountInfoDTO, InsertGroup.class);
                AccountInfoDO accountInfoDO = new AccountInfoDO();
                BeanUtils.copyProperties(accountInfoDTO, accountInfoDO);
                QueryAccountInfo queryAccountInfo = new QueryAccountInfo();
                queryAccountInfo.setAccountNum(accountInfoDTO.getAccountNum());
                ResultDO<List<AccountInfoDO>> selectResult = accountInfoService.selectAccountInfoListResult(queryAccountInfo);
                if (selectResult.isSuccess() && selectResult.getDomain() != null && selectResult.getDomain().size() > 0) {
                    throw ServiceExceptionHelper.buildServiceException(ResultMessageEnum.DATA_RECORD_EXIST, "帐户卡数字信息已存在！");
                }
                accountInfoService.insertAccountInfo(accountInfoDO);
                BeanUtils.copyProperties(accountInfoDO, accountInfoDTO);
            }
        } catch (Exception e) {
            resultDO = ResultHelper.buildResultDOByException(e, accountInfoDTO);
        }
        return resultDO;
    }


    public ResultDO<List<AccountInfoDTO>> saveAccountInfo(List<AccountInfoDTO> list) {
        ResultDO<List<AccountInfoDTO>> resultDO = new ResultDO<>();
        try {
            List<AccountInfoDTO> insertList = new ArrayList<>();
            List<AccountInfoDTO> updateList = new ArrayList<>();
            List<String> uniqueList = new ArrayList<>();
            for (AccountInfoDTO accountInfoDTO : list) {
                if (accountInfoDTO.getAccountInfoId() == null) {
                    insertList.add(accountInfoDTO);
                    ValidationHelper.validateWithThrow(AccountInfoDTO.class, accountInfoDTO, InsertGroup.class);
                } else {
                    updateList.add(accountInfoDTO);
                    ValidationHelper.validateWithThrow(AccountInfoDTO.class, accountInfoDTO, UpdateGroup.class);
                }
                // 唯一性属性
                uniqueList.add(accountInfoDTO.getMail());
            }

            List<String> duplicatesList = ListHelper.findDuplicates(uniqueList);
            if (duplicatesList.size() > 0) {
                //throw new ServiceException(ResultMessageEnum);
            }
            // TODO 数据唯一性校验


        } catch (Exception e) {
            resultDO = ResultHelper.buildResultDOByException(e, null);
        }
        return resultDO;
    }
}